/*
 * Created by Jaren at 2022/10/31 8:56
 */

import 'package:inmotion_protocol/base/base_factory.dart';
import 'package:inmotion_protocol/common/compact.dart';
import 'package:inmotion_protocol/common/product_version.dart';
import 'package:inmotion_protocol/common/vehicle_info.dart';
import 'package:inmotion_protocol/lorin/constant.dart';
import 'package:inmotion_protocol/lorin/factory/special/euc/euc_message_factory.dart';
import 'package:inmotion_protocol/lorin/lorin_message.dart';
import 'package:inmotion_protocol/util/byte_util.dart';

class V13MessageFactory extends EucLorinSpecialMessageFactory<_V13Cmd> implements BMSLogMsgFactoryTrait<LorinMessage>, FullBMSFeatureMsgFactoryTrait<LorinMessage> {
  V13MessageFactory(ProductProperty productProperty) : super(productProperty);

  @override
  void initCompact(int apiVersion, VersionWrapper version) {
    compact = _V13CmdDefault();
  }

  @override
  LorinMessage genSetSpeedLimitMsg(int speedLimit, [int? speedLimit2]) {
    return genSpecialOne2OneWriteMsg(compact!.speedLimitCmd(), ByteUtil.buildBytes(ByteUtil.num2BytesLe(speedLimit, 2), ByteUtil.num2BytesLe(0, 2)));
  }

  LorinMessage genAutoScreenOffOnOffMsg(bool on) {
    return genSpecialOne2OneWriteMsg(compact!.autoScreenOffCmd(), ByteUtil.bool2Bytes(on));
  }

  LorinMessage genSetMaxChargeLevelMsg(int level) {
    return genSpecialMany2ManyReadMsg(null, Constant.deviceEUCV12BLE, compact!.maxChargeLevelCmd());
  }

  LorinMessage genSetMaxDcOutputLevelMsg(int level) {
    return genSpecialMany2ManyReadMsg(null, Constant.deviceEUCV12BLE, compact!.maxDcOutputLevelCmd());
  }

  LorinMessage genSetUsbModeMsg(int usbMode) {
    return genSpecialMany2ManyReadMsg(null, Constant.deviceEUCV12BLE, compact!.usbModeCmd());
  }

  @override
  LorinMessage genRequestBMSRealTimeInfoMsg(int target) {
    return genSpecialMany2ManyReadMsg(null, target, compact!.bmsRealTimeInfoCmd());
  }

  @override
  LorinMessage genRequestBMSFixedInfoMsg(int target) {
    return genSpecialMany2ManyReadMsg(null, target, compact!.bmsFixedInfoCmd());
  }

  @override
  LorinMessage genRequestBMSCellsVoltageInfoMsg(int target) {
    return genSpecialMany2ManyReadMsg(null, target, compact!.bmsCellsVoltageCmd());
  }

  @override
  LorinMessage genRequestBMSDateMsg(int target) {
    return genSpecialMany2ManyReadMsg(null, target, compact!.bmsDateCmd());
  }

  @override
  LorinMessage genRequestBMSLogMsg(int target, int index) {
    return genSpecialMany2ManyReadMsg(null, target, compact!.bmsLogCmd(), ByteUtil.num2BytesLe(index, 4));
  }

  @override
  LorinMessage genRequestBMSLogCountMsg(int target) {
    return genSpecialMany2ManyReadMsg(null, target, compact!.bmsLogCmd(), ByteUtil.num2BytesLe(0xFFFFFFFF, 4));
  }
}

abstract class _V13Cmd extends EucCmdCompact implements FullBMSFeatureCmdTrait {
  int autoScreenOffCmd();

  int usbModeCmd();

  int maxChargeLevelCmd();

  int maxDcOutputLevelCmd();

  int bmsLogCmd();
}

class _V13CmdDefault extends _V13Cmd {
  @override
  int autoHeadlightCmd() {
    return 47;
  }

  @override
  int autoScreenOffCmd() {
    return 61;
  }

  @override
  int autoHeadlightBrightnessThrCmd() {
    return 42;
  }

  @override
  int brightnessOfHeadlightCmd() {
    return 43;
  }

  @override
  int deviceNameCmd() {
    return 12;
  }

  @override
  int headlightOnOffCmd() {
    return 80;
  }

  @override
  int lightEffectIdCmd() {
    return 45;
  }

  @override
  int passwordCmd() {
    return 10;
  }

  @override
  int passwordTypeCmd() {
    return 11;
  }

  @override
  int remainderRangeEstimateCmd() {
    return 61;
  }

  @override
  int soundWaveCmd() {
    return 0;
  }

  @override
  int soundWaveSensitivityCmd() {
    return 0;
  }

  @override
  int speedingBrakingCmd() {
    return 62;
  }

  @override
  int speedingBrakingSensitivityCmd() {
    return 63;
  }

  @override
  int usbPowerCmd() {
    return 60;
  }

  int queryCalibrationResultCmd() {
    return 66;
  }

  @override
  int lightEffectModeCmd() {
    return 45;
  }

  @override
  int bmsCellsVoltageCmd() {
    return 2;
  }

  @override
  int bmsDateCmd() {
    return 3;
  }

  @override
  int bmsFixedInfoCmd() {
    return 4;
  }

  @override
  int bmsLogCmd() {
    return 5;
  }

  @override
  int bmsRealTimeInfoCmd() {
    return 1;
  }

  @override
  int maxChargeLevelCmd() {
    return 66;
  }

  @override
  int maxDcOutputLevelCmd() {
    return 67;
  }

  @override
  int usbModeCmd() {
    return 51;
  }

  @override
  int playSoundItemCmd() {
    return 81;
  }

  @override
  int safeSpeedLimitCmd() {
    return 68;
  }

  @override
  int bermAngleModeCmd() {
    return 69;
  }
}

class V13CmdDelegate extends _V13CmdDefault {}
